#include "../../../common/data_es.hpp"
#include <gflags/gflags.h>
#include <iostream>
#include <memory>
#include <vector>
#include <cassert>

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");
DEFINE_string(es_host, "http://127.0.0.1:9200/", "ES服务器URL");

int main(int argc , char *argv[])
{
    // 解析命令行参数
    google::ParseCommandLineFlags(&argc, &argv, true);

    // 初始化日志系统
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 创建 Elasticsearch 客户端
    auto es_client = mag::ESClientFactory::create({FLAGS_es_host});
    if (!es_client) {
        LOG_ERROR("Elasticsearch 客户端创建失败!");
        return -1;
    }

    //创建 Elasticsearch实例
    auto es_msg = std::make_shared<mag::ESMessage>(es_client);
    assert(es_msg != nullptr);

    // 1. 创建索引
    if (es_msg->createIndex()) {
        LOG_INFO("索引创建测试通过。");
    } else {
        LOG_ERROR("索引创建测试失败。");
        // 继续测试可能会失败，视情况而定
    }

    // 2. 插入数据
    struct TestMessage {
        std::string user_id;
        std::string message_id;
        long create_time;
        std::string chat_session_id;
        std::string content;
    };

    std::vector<TestMessage> test_messages = {
        {"用户ID1", "消息ID1", 1723025035, "会话ID1", "吃饭了吗？"},
        {"用户ID2", "消息ID2", 1723024935, "会话ID1", "吃的盖浇饭！"},
        {"用户ID3", "消息ID3", 1723025035, "会话ID2", "吃饭了吗？"},
        {"用户ID4", "消息ID4", 1723024935, "会话ID2", "吃的盖浇饭！"},
        {"用户ID5", "消息ID5", 1723025036, "会话ID1", "今晚有空吗？"},
        {"用户ID6", "消息ID6", 1723024936, "会话ID3", "盖浇饭在哪里？"}
    };

    for (const auto &msg : test_messages) {
        if (es_msg->appendData(msg.user_id, msg.message_id, msg.create_time, msg.chat_session_id, msg.content)) {
            LOG_INFO("插入消息 {} 成功。", msg.message_id);
        } else {
            LOG_ERROR("插入消息 {} 失败。", msg.message_id);
        }
    }

    // 3. 搜索数据
    std::string search_key = "盖浇";
    std::string search_ssid = "会话ID1";
    auto search_results = es_msg->search(search_key, search_ssid);

    std::cout << "搜索结果数量：" << search_results.size() << std::endl;
    for (const auto &msg : search_results) {
        std::cout << "-----------------" << std::endl;
        std::cout << "用户ID: " << msg.user_id() << std::endl;
        std::cout << "消息ID: " << msg.message_id() << std::endl;
        std::cout << "会话ID: " << msg.session_id() << std::endl;
        std::cout << "创建时间: " << boost::posix_time::to_simple_string(msg.create_time()) << std::endl;
        std::cout << "内容: " << msg.content() << std::endl;
    }

    // 验证搜索结果
    for (const auto &msg : search_results) {
        assert(msg.content().find(search_key) != std::string::npos);
        assert(msg.session_id() == search_ssid);
    }
    LOG_INFO("搜索测试通过。");

    // 4. 删除数据
    std::vector<std::string> delete_message_ids = {"消息ID2", "消息ID4", "消息ID6"};
    for (const auto &mid : delete_message_ids) {
        if (es_msg->remove(mid)) {
            LOG_INFO("删除消息 {} 成功。", mid);
        } else {
            LOG_ERROR("删除消息 {} 失败。", mid);
        }
    }

    // 5. 验证删除操作
    for (const auto &mid : delete_message_ids) {
        auto verify_search = es_msg->search("盖浇", search_ssid);
        bool found = false;
        for (const auto &msg : verify_search) {
            if (msg.message_id() == mid) {
                found = true;
                break;
            }
        }
        assert(!found);
        LOG_INFO("验证删除消息 {} 成功。", mid);
    }

    // 6. 最终搜索验证
    auto final_search = es_msg->search("盖浇", "会话ID1");
    std::cout << "最终搜索结果数量：" << final_search.size() << std::endl;
    for (const auto &msg : final_search) {
        std::cout << "-----------------" << std::endl;
        std::cout << "用户ID: " << msg.user_id() << std::endl;
        std::cout << "消息ID: " << msg.message_id() << std::endl;
        std::cout << "会话ID: " << msg.session_id() << std::endl;
        std::cout << "创建时间: " << boost::posix_time::to_simple_string(msg.create_time()) << std::endl;
        std::cout << "内容: " << msg.content() << std::endl;
    }

    // 确保删除后的搜索结果不包含已删除的消息
    for (const auto &msg : final_search) {
        for (const auto &del_id : delete_message_ids) {
            assert(msg.message_id() != del_id);
        }
    }
    LOG_INFO("删除后搜索验证通过。");

    LOG_INFO("综合测试完成。");

    return 0;
}